package cn.wolfcode.trip.app.controller;

import cn.wolfcode.trip.base.domain.User;
import cn.wolfcode.trip.base.service.IUserService;
import cn.wolfcode.trip.base.util.JSONResult;
import cn.wolfcode.trip.base.util.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 登录用户资源控制器
 */
@RestController
@RequestMapping("sessions")
public class SessionController {
    @Autowired
    private IUserService userService;

    /**
     * 用户登录接口
     * @param user
     * @return
     */
    @PostMapping
    public JSONResult login(User user){
        JSONResult jsonResult = new JSONResult();
        try {
            User temp = userService.login(user);
            jsonResult.setResult(temp);//侧边栏回显
        } catch (Exception e) {
            e.printStackTrace();
            jsonResult.mark(e.getMessage());
        }
        return jsonResult;
    }

    /**
     * 用户注销接口
     */
    @DeleteMapping
    public void logout(){
        System.out.println("注销成功!");
        UserContext.getSession().invalidate();
    }
}
